package zhash

import (
	"bytes"
	"reflect"
)

// GetBKDR Hash一个字符串
func GetBKDR(s string) uint32 {
	h, isExist := getFromCache(s)
	if isExist {
		return h
	}

	h = getBKDRHash(bytes.NewBufferString(s).Bytes())
	saveToCache(s, h)
	return h
}

func MsgHash(msg interface{}) uint32 {
	t := reflect.TypeOf(msg)
	if t.Kind() == reflect.Struct {
		name := t.Name()
		bkdr := GetBKDR(name)
		return bkdr
	}
	name := t.Elem().Name()
	bkdr := GetBKDR(name)
	return bkdr
}

func GetMsgFromHash(hash uint32) string {
	locker.RLock()
	defer locker.RUnlock()
	return reverseCache[hash]
}
